package com.neusoft.yuejian_6.fragment;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.neusoft.yuejian_6.R;
import com.neusoft.yuejian_6.activity.FeedBackActivity;
import com.neusoft.yuejian_6.activity.GuanyuActivity;
import com.neusoft.yuejian_6.activity.InformActivity;
import com.neusoft.yuejian_6.activity.ModifyUserActivity;
import com.neusoft.yuejian_6.activity.MyPostActivity;
import com.neusoft.yuejian_6.activity.OrderActivity;
import com.neusoft.yuejian_6.activity.PrizeActivity;
import com.neusoft.yuejian_6.javabean.User;
import com.neusoft.yuejian_6.model.UserModel;
import com.squareup.picasso.Picasso;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 an instance of this fragment.
 */
public class MyFragment extends Fragment {
    private TextView user_name1;
    private ImageView user_pic;
    private ImageButton id_guanyu;
    private ImageButton id_feedback;
    private ImageButton id_updatemy;
    private ImageButton id_tiezi;
    private ImageButton id_order;
    private ImageButton id_prize;
    private ImageButton id_inform;
    private View view=null;
    private int user_id;


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState)
    {
        view=inflater.inflate(R.layout.tab05, container, false);
        initViews();
        initData();
        initEvent();
        return view;
    }
    public void initViews(){
        // back=(ImageView)view.findViewById(R.id.back);
        user_name1=(TextView)view.findViewById(R.id.user_name);
        user_pic=(ImageView)view.findViewById(R.id.user_pic);
        id_updatemy=(ImageButton)view.findViewById(R.id.id_updatemy);
        id_guanyu=(ImageButton)view.findViewById(R.id.id_guanyu);
        id_feedback=(ImageButton)view.findViewById(R.id.id_feedback);
        id_tiezi=(ImageButton)view.findViewById(R.id.id_tiezi);
        id_prize=(ImageButton)view.findViewById(R.id.id_prize);
        id_inform=(ImageButton)view.findViewById(R.id.id_inform);
        id_order=(ImageButton)view.findViewById(R.id.id_dingdan);



    }

    public void initEvent(){

        id_updatemy.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),ModifyUserActivity.class);
                startActivity(intent1);

            }
        });
        id_guanyu.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),GuanyuActivity.class);
                startActivity(intent1);

            }
        });
        id_feedback.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),FeedBackActivity.class);
                startActivity(intent1);

            }
        });
        id_tiezi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),MyPostActivity.class);
                startActivity(intent1);

            }
        });
        id_prize.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),PrizeActivity.class);
                startActivity(intent1);

            }
        });
        id_order.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),OrderActivity.class);
                startActivity(intent1);

            }
        });
        id_inform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent1=new Intent();
                intent1.setClass(MyFragment.this.getActivity(),InformActivity.class);
                startActivity(intent1);

            }
        });
    }




    public void initData(){
        SharedPreferences sp=getActivity().getSharedPreferences("userInfo", Context.MODE_PRIVATE);
        user_id = sp.getInt("user_id",0);
        //  Log.d("userbg","u_id");

        UserModel userModel = new UserModel();
        Call call = userModel.getUserInfo(user_id);
        call.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Call<User> call, Response<User> response) {
                User user = response.body();
                user_name1.setText(user.getUser_name());
                //    Log.d("userbg","username");
                String upic="http://10.0.2.2/neu1_yuejian/Public/Admin/photo/"+user.getUser_pic();
                Picasso.with(MyFragment.this.getActivity()).load(upic).into(user_pic);
            }

            @Override
            public void onFailure(Call<User> call, Throwable t) {
            }
        });
    }

}

